#define _CRT_SECURE_NO_WARNINGS 1
#include "BinaryTree.h"
//初始化队列
void QueueInit(Queue* ps)
{
	assert(ps);
	ps->head = NULL;
	ps->tail = NULL;
	ps->size = 0;

}
//进队列
void QueuePush(Queue* ps, DataType x)
{
	assert(ps);
	//申请节点
	Qnode* newnode =(Qnode*)malloc(sizeof(Qnode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->val = x;
	newnode->next = NULL;
	//判断是否为第一次进队列
	if (ps->tail == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	ps->size++;
}
//出队列
void QueuePop(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	//只有一个元素时
	if (ps->head == ps->tail)
	{
		free(ps->head);
		ps->head = ps->tail = NULL;
	}
	else
	{
		Qnode* next = ps->head->next;
		free(ps->head);
		ps->head = next;
	}
	ps->size--;
}
//销毁队列
void QueueDestory(Queue* ps)
{
	assert(ps);

	Qnode* cur = ps->head;
	while (cur)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->head = ps->tail = NULL;
	ps->size = 0;
}
//获取队列头元素
DataType QueueFront(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->head->val;
}
//获取队列尾元素
DataType QueueBack(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->tail->val;
}
//检测队列是否为空
bool QueueEmpty(Queue* ps)
{
	assert(ps);
	return ps->head == NULL;
}
//获取队列有效元素个数
int QueueSzie(Queue* ps)
{
	assert(ps);
	return ps->size;
}









